package latch;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class WorkerThread extends Thread{

	private CountDownLatch mDownLatch;
	private String mName;

	public WorkerThread(CountDownLatch downLatch, String name){
		mDownLatch = downLatch;
		mName = name;
	}

	public void run() {
		doWork();
		try{
			TimeUnit.SECONDS.sleep(new Random().nextInt(10));
		}catch(InterruptedException ie){
		}finally{
			System.out.println(mName + " finish the job");
			mDownLatch.countDown();
		}
	}

	private void doWork(){
		System.out.println(mName + " is working...");
	}

}
